package top.zsyle.basic.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.zsyle.basic.dto.EmailCodeDto;
import top.zsyle.basic.dto.SmsCodeDto;
import top.zsyle.basic.service.VerifyCodeService;
import top.zsyle.basic.util.AjaxResult;

import javax.validation.Valid;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private VerifyCodeService verifyCodeService;

    /**
     * 请求获取图片验证码
     * @param imageCodeKey
     * @return
     */
    @GetMapping("/image/{imageCodeKey}")
    public AjaxResult graph(@PathVariable("imageCodeKey") String imageCodeKey){

        String base64Graph = verifyCodeService.graph(imageCodeKey);

        return AjaxResult.me().success(base64Graph);
    }

    /**
     * 请求获取短信验证码
     * @param dto
     * @return
     */
    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody @Valid SmsCodeDto dto){
        verifyCodeService.smsCode(dto);
        return AjaxResult.me().success();
    }

    /**
     * 短信验证码登录：请求验证码
     * @param dto
     * @return
     */
    @PostMapping("/smsCodeLogin")
    public AjaxResult smsCodeLogin(@RequestBody @Valid SmsCodeDto dto){
        verifyCodeService.smsCodeLogin(dto);
        return AjaxResult.me().success();
    }

    /**
     * 请求获取 邮箱验证码
     * @param dto
     * @return
     */
    @PostMapping("/emailCode")
    public AjaxResult emailCode(@RequestBody @Valid EmailCodeDto dto){
        verifyCodeService.emailCode(dto);
        return AjaxResult.me().success();
    }
}
